package Yangtz.Cs.Reader.Controller.Borrow;

import Yangtz.Cs.Entity.BorrowBean;
import Yangtz.Cs.Service.BorrowService;
import Yangtz.Cs.Utils.DataTransmissionUtil;
import Yangtz.Cs.Utils.SetEncodingUtil;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/BorrowServlet")
public class BorrowServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置请求编码、响应方式和编码方式
        SetEncodingUtil.SetEncoding(request, response);

        //声明JSONObject对象
        JSONObject jsonObject1;     //用来接收android传来的数据

        //通过gainJsonFromAndroid(request)获取Android客户端发来的json数据，并将它解析
        jsonObject1 = DataTransmissionUtil.gainJsonFromAndroid(request);

        BorrowBean borrowBean = new BorrowBean();

        //将从android传来的数据给到borrowBean
        String bkID=jsonObject1.optString("bkID");
        String rdID=jsonObject1.optString("rdID");

        borrowBean.setBkID(bkID);
        borrowBean.setRdID(rdID);

        BorrowService borrowService=new BorrowService();
        //调用该方法借书，改变数据库中相应的值
        borrowService.borrow(borrowBean,response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
